package com.classroom.smart.service;

import com.classroom.smart.mapper.TeacherMapper;
import com.classroom.smart.pojo.Teacher;
import com.classroom.smart.pojo.TeacherExample;
import com.classroom.smart.vo.LoginVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:YouGuoyun
 * @Title:leetcode
 * @Date:Create in 2023/2/23 20:00
 **/
@Service
public class TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    public List<Teacher> viewTeacherByQuery( int current,String query){
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andNameLike("%" + query + "%");
        PageHelper.startPage(current , 3);
        List<Teacher> list = teacherMapper.selectByExample(teacherExample);
        return list;
    }

    public Teacher login(LoginVO vo){
        TeacherExample teacherExample = new TeacherExample();
        teacherExample.createCriteria().andNameEqualTo(vo.getUsername()).andPasswordEqualTo(vo.getPassword());
        List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
        return teachers.get(0);
    }

    //删除操作
    public boolean delete(int teacherId) {
        int i = teacherMapper.deleteByPrimaryKey(teacherId);
        return i>0?true:false;
    }

    //根据ID查询用户信息
    public Teacher viewOne(int teacherId) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(teacherId);
        return teacher;
    }

    //更新用户信息
    public boolean update(Teacher teacher){
        int i = teacherMapper.updateByPrimaryKeySelective(teacher);
        return i>0?true:false;
    }

    //添加用户
    public boolean addOne(Teacher teacher){
        int i = teacherMapper.insert(teacher);
        return i>0;
    }


}
